<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class FormController extends HomeController {

		/* 自定义表单 */

    public function add(){
        $category = $this->category();
        //获取模型信息
      //  die(var_dump($_POST));
        if($_GET['model_id']){
            $model_id=$_GET['model_id'];
        }else{
            $model_id=$category['model'][0];
        }
        $model = M('Model')->where(array('status' => 1))->find($model_id);
        $model || $this->error('模型不存在！');
        if(IS_POST){
            /*if($_POST['code']){
                if($_POST['code']!=$_SESSION['userinfo_code']){
                    echo "1";
                    die;
                }
            }else{
                echo "0";
                die;
            }*/
            $Model  =   D(parse_name(get_table_name($model['id']),1));
            // 获取模型的字段信息
            $Model  =   $this->checkAttr($Model,$model['id']);
            // die(var_dump($Model));
            if($Model->create() && $Model->add()){
                unset ($_SESSION['userinfo_code']);
                $this->success('添加'.$model['title'].'成功！', U('lists?model='.$model['name']));
            } else {
                $this->error($Model->getError());
            }
        } else {

            $fields = get_model_attribute($model['id']);
            $this->assign('model', $model);
            $this->assign('category', $category);
            $this->assign('fields', $fields);
            $this->meta_title = '新增'.$model['title'];
            $this->display($category['template_index']);
        }
    }	
    public function resume_add(){
		$category = $this->category();
        //获取模型信息
      //  die(var_dump($_POST));
        if($_GET['model_id']){
            $model_id=$_GET['model_id'];
        }else{
            $model_id=$category['model'][0];
        }
        $model = M('Model')->where(array('status' => 1))->find($model_id);
        $model || $this->error('模型不存在！');
        if(IS_POST){
            if(!check_verify(I("post.verify"))){
                $this->error('验证码输入错误！');
            }
            $Model  =   D(parse_name(get_table_name($model['id']),1));
            // 获取模型的字段信息
            $Model  =   $this->checkAttr($Model,$model['id']);
            // die(var_dump($Model));
            if($Model->create() && $Model->add()){
                unset ($_SESSION['userinfo_code']);
                $this->success('添加'.$model['title'].'成功！', U('lists?model='.$model['name']));
            } else {
                $this->error($Model->getError());
            }
        } else {

            $fields = get_model_attribute($model['id']);
            $this->assign('model', $model);
            $this->assign('category', $category);
            $this->assign('fields', $fields);
            $this->meta_title = '新增'.$model['title'];
            $this->display($category['template_index']);
        }
    }
    protected function checkAttr($Model,$model_id){
        $fields     =   get_model_attribute($model_id,false);
        $validate   =   $auto   =   array();
        foreach($fields as $key=>$attr){
            if($attr['is_must']){// 必填字段
                $validate[]  =  array($attr['name'],'require',$attr['title'].'必须!');
            }
            // 自动验证规则
            if(!empty($attr['validate_rule'])) {
                $validate[]  =  array($attr['name'],$attr['validate_rule'],$attr['error_info']?$attr['error_info']:$attr['title'].'验证错误',0,$attr['validate_type'],$attr['validate_time']);
            }
            // 自动完成规则
            if(!empty($attr['auto_rule'])) {
                $auto[]  =  array($attr['name'],$attr['auto_rule'],$attr['auto_time'],$attr['auto_type']);
            }elseif('checkbox'==$attr['type']){ // 多选型
                $auto[] =   array($attr['name'],'arr2str',3,'function');
            }elseif('date' == $attr['type']){ // 日期型
                $auto[] =   array($attr['name'],'strtotime',3,'function');
            }elseif('datetime' == $attr['type']){ // 时间型
                $auto[] =   array($attr['name'],'strtotime',3,'function');
            }
        }
        return $Model->validate($validate)->auto($auto);
    }
    public function sendsms($usertel,$code){
		if($usertel>=13000000000 && $user<=19999999999){
			if(session('?user_code') && session('user_code.time')+60>time())
				$data['info']="请在".(session('user_code.time')+60-time())."秒后重试";
			else{
				$data['code']=rand(100000,999999);
				$data['time']=time();
				$data['info']="获取验证码成功";
				$data['times']=6;
				sendsms($usertel,"您的验证码是".$data['code']);
				session('user_code',$data);
			}
		}
		else $data['info']="请输入正确电话号码";
        //$_SESSION['userinfo_code']=$code;
        echo $data['info'];//sendsms($usertel,"您的验证码是".$data['code']);
    }
	public function sendsms_tel(){
		$Model=M();
		$rules=array(
			array('usertel','require','必须填写电话号！',1),
			array('usertel',array(13000000000,19999999999),'请输入正确电话号码',1,'between',3),
			array('code','require','请输入验证码',1),
			array('username','require','请输入用户名',1),
			array('username','1,10','用户名长度超出限制',1,'length'),
			array('usercompany','require','请输入公司名',1),
			array('usercompany','1,30','公司名长度超出限制',1,'length'),
			array('cpgw_name','require','出现未知错误',1),
			array('cpgw_id','require','出现未知错误',1),
			array('cpgw_tel','require','您选择的销售顾问没有录入电话,请拨打85523668与我司联系',1),
		);
		$result=$Model->validate($rules)->create();
		session('user_code.times',session('user_code.times')-1);
		if(session('user_code.times')==0)$data['message']="超过最大尝试次数，请重新获取验证码";
		else if(session('user_code.time')+120<time())$data['message']="超时，请重新获取验证码";
		else if(I('code')!=session('user_code.code'))$data['message']="验证码错误";
		else if($result!=0){
			$sms_data['addtime']=time();
			$sms_data['cpgw_name']=I('cpgw_name');
			$sms_data['cpgw_id']=I('cpgw_id');
			$sms_data['usertel']=I('usertel');
			$sms_data['usercompany']=I('usercompany');
			$sms_data['username']=I('username');
			
			$Insms=D('userinfo');
			$Insms->add($sms_data);
			sendsms(I("cpgw_tel"),I("cpgw_name")."您好,在官网有一个客户咨询您了,快给客户回个电话吧,联系人:".I("username").",电话:".I("usertel"));
			$data['message']="您的信息已提交成功，稍后 ".I('cpgw_name')." 会联系您，请保持电话畅通！";
            session('user_code',array());
		}else
			$data['message']=$Model->getError();
		$data['code']=1;
		$this->ajaxReturn($data);
		//$this->ajaxReturn(I('data')['username']);//1,3
		//$this->ajaxReturn(I('post.data'));
		//$_SESSION['userinfo_code']=$code;
        //echo sendsms($usertel,$code);
    }
}
